package com.example.booksdeal.basic;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.example.booksdeal.BmobDBO.BooksDetail;
import com.example.booksdeal.BmobDBO.Collect;
import com.example.booksdeal.BmobDBO.User;
import com.example.booksdeal.R;
import com.example.booksdeal.util.DBOpenHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SQLQueryListener;
import cn.bmob.v3.listener.UpdateListener;

public class MyPushActivity extends AppCompatActivity {

    private DBOpenHelper dbOpenHelper;  //声明DBOpenHelper对象
    private SQLiteDatabase db;
    private ListView booklist;
    private String username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_push);
        dbOpenHelper = new DBOpenHelper(this, "db_booksdeal.db", null, 1);  //实例化DBOpenHelper对象，用来创建数据库
        db = dbOpenHelper.getWritableDatabase();
        Bundle bundle = getIntent().getExtras();
        username = bundle.getString("username");
        initview();
    }

    public void initview() {
        booklist = findViewById(R.id.bookslist);

        ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
        BmobQuery<BooksDetail> query = new BmobQuery<BooksDetail>();
        //query.setPreparedParams(new Object[]{username});
        query.addWhereEqualTo("pushname", User.getCurrentUser(User.class));
        query.order("-updatedAt");
        query.findObjects(new FindListener<BooksDetail>() {
            @Override
            public void done(List<BooksDetail> list, BmobException e) {
                if (e == null) {
                    if (list != null && list.size() > 0) {
                        for (int i = 0; i < list.size(); i++) {
                            HashMap<String, Object> map = new HashMap<String, Object>();
                            map.put("gid",list.get(i).getObjectId());
                            map.put("id", list.get(i).getBook_id());
                            map.put("name", list.get(i).getBook_name());
                            map.put("price", list.get(i).getBook_price());
                            map.put("time", list.get(i).getCreatedAt());
                            listItem.add(map);
                            SimpleAdapter simpleAdapter = new SimpleAdapter(MyPushActivity.this, listItem, R.layout.activity_book_item,
                                    new String[]{"id", "name", "price", "time"}, new int[]{R.id.gid, R.id.bname, R.id.price, R.id.time});
                            booklist.setAdapter(simpleAdapter);
                            booklist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                    Intent intent = new Intent(MyPushActivity.this, BooksDetailActivity.class);
                                    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);  //只启动一个activity
                                    Bundle bundle = new Bundle();
                                    bundle.putString("id", listItem.get(i).get("gid").toString());
                                    bundle.putString("username",username);
                                    intent.putExtras(bundle);
                                    startActivity(intent);
                                }
                            });
                            booklist.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                                @Override
                                public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                                    AlertDialog.Builder normalDialog = new AlertDialog.Builder(MyPushActivity.this);
                                    normalDialog.setTitle("是否下架图书?");
                                    normalDialog.setPositiveButton("确定",
                                            new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    BooksDetail booksDetail = new BooksDetail();
                                                    booksDetail.setObjectId(listItem.get(i).get("gid").toString());
                                                    booksDetail.delete(new UpdateListener() {

                                                        @Override
                                                        public void done(BmobException e) {
                                                            if(e==null){
                                                                Toast.makeText(MyPushActivity.this,"下架成功",Toast.LENGTH_LONG).show();
                                                            }
                                                        }
                                                    });
                                                }
                                            });
                                    normalDialog.setNegativeButton("关闭",
                                            new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    //...To-do
                                                }
                                            });
                                    // 显示
                                    normalDialog.show();
                                    return true;
                                }
                            });
                        }
                    }
                }
            }
        });

//        Cursor cursor = db.query("BooksDetail", new String[]{"id", "book_name", "book_price", "time"}, "username=?", new String[]{username}, null, null, null);
//        if (cursor != null && cursor.getCount() > 0) {
//            while (cursor.moveToNext()) {
//                HashMap<String, Object> map = new HashMap<String, Object>();
//                map.put("id", cursor.getInt(cursor.getColumnIndex("id")));
//                map.put("name", cursor.getString(cursor.getColumnIndex("book_name")));
//                map.put("price", cursor.getString(cursor.getColumnIndex("book_price")));
//                map.put("time", cursor.getString(cursor.getColumnIndex("time")));
//                listItem.add(map);
//                SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItem, R.layout.activity_book_item,
//                        new String[]{"id", "name", "price", "time"}, new int[]{R.id.gid, R.id.bname, R.id.price, R.id.time});
//
//                booklist.setAdapter(simpleAdapter);
//                booklist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                    @Override
//                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                        Intent intent = new Intent(MyPushActivity.this, BooksDetailActivity.class);
//                        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);  //只启动一个activity
//                        Bundle bundle = new Bundle();
//                        bundle.putString("id", listItem.get(i).get("id").toString());
//                        bundle.putString("username",username);
//                        intent.putExtras(bundle);
//                        startActivity(intent);
//                    }
//                });
//                booklist.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
//                    @Override
//                    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
//                        AlertDialog.Builder normalDialog = new AlertDialog.Builder(MyPushActivity.this);
//                        normalDialog.setTitle("是否下架图书?");
//                        normalDialog.setPositiveButton("确定",
//                                new DialogInterface.OnClickListener() {
//                                    @Override
//                                    public void onClick(DialogInterface dialog, int which) {
//                                        db.execSQL("delete from BooksDetail where BooksDetail.id=?", new String[]{listItem.get(i).get("id").toString()});
//                                        db.execSQL("delete from Comment where Comment.cmt_id=?", new String[]{listItem.get(i).get("id").toString()});
//                                        db.close();
//                                        Toast.makeText(MyPushActivity.this,"下架成功",Toast.LENGTH_LONG).show();
//                                    }
//                                });
//                        normalDialog.setNegativeButton("关闭",
//                                new DialogInterface.OnClickListener() {
//                                    @Override
//                                    public void onClick(DialogInterface dialog, int which) {
//                                        //...To-do
//                                    }
//                                });
//                        // 显示
//                        normalDialog.show();
//                        return true;
//                    }
//                });
//            }
//        }
    }


}